package com.likai.jarscms.luence;

import com.likai.jarscms.pojo.JarBean;
import com.likai.jarscms.utils.StringUtil;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.StringReader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by likai on 2018/10/19.
 */
public class JarsIndex {

    private Directory dir ;

    private static final String LUENCE_PATH = "D:" + File.separator + "luence" ;

    /**
     * 查询jar包信息
     * @param q 关键字
     * @param n 查询多少条
     * @return
     * @throws Exception
     */
    public List<JarBean> searchJar(String q, int n) throws Exception {
        //取得路径
        dir = FSDirectory.open(Paths.get("D://lucene")) ;
        //实例化IndexReader
        IndexReader reader = DirectoryReader.open(dir) ;
        //实例化IndexSearcher
        IndexSearcher is = new IndexSearcher(reader) ;

        Analyzer analyzer = new StandardAnalyzer() ;
        QueryParser parser = new QueryParser("name",analyzer) ;

        Query query = parser.parse(q) ;

        //进行查询
        //	n条最佳得分记录
        TopDocs hits = is.search(query,n) ;
        //	取得得分片段并关键字高亮显示
        QueryScorer scorer = new QueryScorer(query) ;
        Fragmenter fragmenter = new SimpleSpanFragmenter(scorer) ;
        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b><font color='red'>","</font></b>") ;
        Highlighter highlighter = new Highlighter(formatter, scorer);
        highlighter.setTextFragmenter(fragmenter);

        List<JarBean> jarList = new ArrayList<JarBean>() ;
        //遍历并给list赋值
        for (ScoreDoc scoreDoc : hits.scoreDocs) {
            Document document = is.doc(scoreDoc.doc) ;
            JarBean jar = new JarBean() ;
            jar.setUuid(document.get("id"));
            String name = document.get("name") ;
            jar.setNoTagName(name) ;
            //取得最佳片段
            if (name != null) {
                TokenStream tokenStream = analyzer.tokenStream("name",new StringReader(name)) ;
                //高亮之后的name
                String hName = highlighter.getBestFragment(tokenStream,name) ;
                //如果hName不为null把高亮之后的代码赋给name,否则赋值为普通的
                if (StringUtil.isEmpty(hName)) {
                    jar.setName(name);
                } else {
                    jar.setName(hName);
                }
            }

            jarList.add(jar) ;

        }

        return jarList ;
    }


    public static void main(String [] args) throws Exception {
        List<JarBean> jarList = new JarsIndex().searchJar("jvm",20) ;
        for (JarBean jarBean : jarList) {
            System.out.println(jarBean.getUuid());
            System.out.println(jarBean.getNoTagName());
            System.out.println(jarBean.getName());

            System.out.println("----------------------------");
        }
    }

}
